﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using UmlControlLibrary;

namespace UmlControlLibrary
{
    public class ResizeThumb : Thumb
    {
        const int MIN_WIDTH = 100;
        const int MIN_HEIGHT = 100;
        private ResizerPosition position;
        public ResizerPosition Position
        {
            get { return position; }
            set { position = value; }
        }

        public ResizeThumb()
        {
            base.DragDelta += new DragDeltaEventHandler(ResizeThumb_DragDelta);
            base.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(ResizeThumb_MouseDoubleClick);
            base.ToolTip = "Drag to resize, double-click to revert to auto size";
        }

        Point startmousepos;

        void ResizeThumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            startmousepos = Mouse.GetPosition(this);
        }

        void ResizeThumb_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            UmlControl item = this.DataContext as UmlControl;

            if (item != null)
            {
                item.Height = Double.NaN;
                item.Width = Double.NaN;
                item.UpdateLayout();
                if (item.RenderSize.Height > 400)
                    item.Height = 400;
                if (item.RenderSize.Width > 300)
                    item.Width = 300;
                item.Resized();
            }
            e.Handled = true;
        }

        void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            UmlControl item = this.DataContext as UmlControl;

            double horiChange, vertChange;
//            Point mousePos = Mouse.GetPosition(this);
         //   horiChange = /*e.HorizontalChange +*/ mousePos.X - startmousepos.X;
         //   vertChange = /*e.VerticalChange +*/ mousePos.Y - startmousepos.Y;

            horiChange = SelectableItemControl.AlignToGrid(e.HorizontalChange);
            vertChange = SelectableItemControl.AlignToGrid(e.VerticalChange);

            if (item != null)
            {
                switch (this.Position)
                {
                    case ResizerPosition.Top:
                        Canvas.SetTop(item, Canvas.GetTop(item) + vertChange);
                        item.Height = Math.Max(MIN_HEIGHT, item.ActualHeight - vertChange);
                        break;
                    case ResizerPosition.Bottom:
                        item.Height = Math.Max(MIN_HEIGHT, item.ActualHeight + vertChange);
                        break;
                    case ResizerPosition.Left:
                        Canvas.SetLeft(item, Canvas.GetLeft(item) + horiChange);
                        item.Width = Math.Max(MIN_WIDTH, item.ActualWidth - horiChange);
                        break;
                    case ResizerPosition.Right:
                        item.Width = Math.Max(MIN_WIDTH, item.ActualWidth + horiChange);
                        break;
                    case ResizerPosition.TopLeft:
                        Canvas.SetTop(item, Canvas.GetTop(item) + vertChange);
                        item.Height = Math.Max(MIN_HEIGHT, item.ActualHeight - vertChange);
                        Canvas.SetLeft(item, Canvas.GetLeft(item) + horiChange);
                        item.Width = Math.Max(MIN_WIDTH, item.ActualWidth - horiChange);
                        break;
                    case ResizerPosition.TopRight:
                        Canvas.SetTop(item, Canvas.GetTop(item) + vertChange);
                        item.Height = Math.Max(MIN_HEIGHT, item.ActualHeight - vertChange);
                        item.Width = Math.Max(MIN_WIDTH, item.ActualWidth + horiChange);
                        break;
                    case ResizerPosition.BottomLeft:
                        item.Height = Math.Max(MIN_HEIGHT, item.ActualHeight + vertChange);
                        Canvas.SetLeft(item, Canvas.GetLeft(item) + horiChange);
                        item.Width = Math.Max(MIN_WIDTH, item.ActualWidth - horiChange);
                        break;
                    case ResizerPosition.BottomRight:
                        item.Height = Math.Max(MIN_HEIGHT, item.ActualHeight + vertChange);
                        item.Width = Math.Max(MIN_WIDTH, item.ActualWidth + horiChange);
                        break;
                    default:
                        break;
                }
                item.Resized();
//                item.OwnerDiagram.DisableCanvasDrag();
            }
            e.Handled = true;

        }
    }

    public enum ResizerPosition
    {
        None,
        Top,
        Bottom,
        Left,
        Right,
        TopLeft,
        TopRight,
        BottomLeft,
        BottomRight
    }
}
